package com.kgc.filter;


import com.kgc.utils.Constants;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName LoginBootFilter
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2019/6/10 16:40
 * @Version 1.0
 **/
@WebFilter(urlPatterns = {"/view/*"})
public class LoginBootFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        Object user = request.getSession().getAttribute(Constants.USER_LOGIN_KEY);
        if(user != null){//
            filterChain.doFilter(servletRequest, servletResponse);
        }else{//
            HttpServletResponse response = (HttpServletResponse)servletResponse;
            String contextPath = request.getSession().getServletContext().getContextPath();
            response.sendRedirect(contextPath+"/login.html");
        }
    }

    @Override
    public void destroy() {

    }
}
